package com.negative.activities;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;

import com.negative.data.EventItem;
import com.negative.logic.AutoCompleteEditTextContainAdapter;
import com.negative.logic.FilterEventByDate;
import com.negative.logic.MenuListener;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

public class SearchEventActivity extends Activity implements OnClickListener,
		OnDateSetListener, OnItemClickListener {

	private NegativeApplication app;
	private ArrayList<EventItem> events;

	AutoCompleteTextView textView;
	EditText fromDateText, toDateText;
	ImageView fromCal, toCal;
	ImageView menuButton;
	Button searchButton;
	DatePickerDialog fromDateDialog, toDateDialog;
	Calendar cal;
	Date fromDate, toDate;
	SimpleDateFormat df, dateWithTime;
	Context context;
	MenuListener menuListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_event_layout);

		initialize();

		cal = Calendar.getInstance();
		df = new SimpleDateFormat("dd/MM/yyyy", new Locale("iw-il"));
		dateWithTime = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm", new Locale("iw-il"));
		fromDate = cal.getTime();
		fromDateText.setText(df.format(fromDate));
		fromDateText.setTextColor(Color.BLACK);
		cal.add(Calendar.DAY_OF_YEAR, +7);
		toDate = cal.getTime();
		toDateText.setText(df.format(toDate));
		toDateText.setTextColor(Color.BLACK);

		InitSearchByName();
	}

	private void InitSearchByName() {

		ArrayList<String> arrStr = new ArrayList<String>(events.size());
		for (int i = 0; i < events.size(); i++) {
			arrStr.add(events.get(i).getName());
		}

		AutoCompleteEditTextContainAdapter customeAdapter = new AutoCompleteEditTextContainAdapter(
				this, android.R.layout.simple_dropdown_item_1line, arrStr);
		textView.setAdapter(customeAdapter);
		textView.setThreshold(2);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(searchButton)) {
			FilterEventByDate eventFilter = new FilterEventByDate(fromDate,
					toDate);
			Intent intent = new Intent(this, EventListActivity.class);
			intent.putExtra("filter", eventFilter);
			startActivity(intent);
		}
		cal = Calendar.getInstance();
		if (v.equals(fromCal) || v.equals(fromDateText)) {
			fromDateDialog = new DatePickerDialog(v.getContext(), this,
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DAY_OF_MONTH));
			fromDateDialog.getDatePicker().setCalendarViewShown(false);
			fromDateDialog.show();
		} else if (v.equals(toCal) || v.equals(toDateText)) {
			toDateDialog = new DatePickerDialog(v.getContext(), this,
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
					cal.get(Calendar.DAY_OF_MONTH));
			toDateDialog.getDatePicker().setCalendarViewShown(false);
			toDateDialog.show();
		}
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		Date date = null;
		try {
			String dateAsString = dayOfMonth + "/" + (monthOfYear + 1) + "/"
					+ year;
			date = df.parse(dateAsString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (fromDateDialog != null && view.equals(fromDateDialog.getDatePicker())) {

			fromDate = date;
			fromDateText.setText(df.format(fromDate));

		} else if (toDateDialog != null && view.equals(toDateDialog.getDatePicker())) {

			toDate = date;
			toDateText.setText(df.format(toDate));
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View arg1, int position,
			long arg3) {
		String name = (String) adapter.getItemAtPosition(position);
		final HashMap<String, EventItem> eventMap = new HashMap<String, EventItem>();
		for (EventItem event : events) {
			if (event.getName().equals(name))
				eventMap.put(dateWithTime.format(event.getStartDate()), event);
		}

		String[] dates = eventMap.keySet().toArray(new String[eventMap.values().size()]);
		Date[] dateArr = new Date[dates.length];
		for (int i = 0; i < dates.length; i++) {
			try {
				dateArr[i] = dateWithTime.parse(dates[i]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
		}
//		Arrays.sort(dates);
		Arrays.sort(dateArr);
		for (int i = 0; i < dates.length; i++) {
			dates[i] = dateWithTime.format(dateArr[i]);
		}
		ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_list_item_1, dates);
		
		final Dialog dateDialog = new Dialog(context);
		dateDialog.setContentView(R.layout.event_date_list_layout);
		dateDialog.setTitle(R.string.chooseDate);
		ListView eventDates = (ListView) dateDialog
				.findViewById(R.id.event_date_list);
		eventDates.setAdapter(listAdapter);
		eventDates.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String date = (String)arg0.getItemAtPosition(arg2);
				EventItem event = eventMap.get(date);
				Intent intent = new Intent(context, EventActivity.class);
				intent.putExtra("EventItem", event);
				startActivity(intent);
				dateDialog.dismiss();
			}
		});
		dateDialog.show();
	}

	private void initialize() {
		context = this;
		app = (NegativeApplication) this.getApplication();
		events = app.getEventList();
		menuListener = new MenuListener(this);

		textView = (AutoCompleteTextView) findViewById(R.id.autoedit);
		fromDateText = (EditText) findViewById(R.id.fromEditText);
		toDateText = (EditText) findViewById(R.id.toEditText);
		fromCal = (ImageView) findViewById(R.id.from_calendar);
		toCal = (ImageView) findViewById(R.id.to_calendar);
		searchButton = (Button) findViewById(R.id.searchButton);
		menuButton = (ImageView) findViewById(R.id.menu_icon);

		fromDateText.setOnClickListener(this);
		toDateText.setOnClickListener(this);
		fromCal.setOnClickListener(this);
		toCal.setOnClickListener(this);
		searchButton.setOnClickListener(this);
		textView.setOnItemClickListener(this);
		menuButton.setOnClickListener(menuListener);
		
	}
	
}
